/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import utils.DBUtils;
import xmlasm.dto.Feedbacks;
import xmlasm.dto.Feedback;

/**
 *
 * @author Hoang
 */
public class FeedbackDAO {

    public static Feedbacks getAllFeedbacks() {
        PreparedStatement stm = null;
        ResultSet rs = null;
        Connection con = null;

        try {
            con = DBUtils.makeConnection();
            String sql = "SELECT * FROM FEEDBACK";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            Feedbacks fbs = new Feedbacks();
            while (rs.next()) {
                int feedbackId = rs.getInt("feedbackId");
                int userId = rs.getInt("userId");
                String title = rs.getString("title");
                String feedbackContent = rs.getString("feedbackContent");
                Date date = rs.getTimestamp("sendDate");
                Feedback fb = new Feedback(feedbackId, userId, title, feedbackContent, date);
                fbs.getFeedback().add(fb);
            }
            return fbs;
        } catch (SQLException ex) {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("SQLErrors", ex.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("SQLErrors", e.getMessage());
            }
        }
        return null;
    }

    public static boolean deleteFeedback(int id) {
        PreparedStatement stm = null;
        Connection con = null;
        try {
            con = DBUtils.makeConnection();
            String sql = "DELETE FROM FEEDBACK WHERE feedbackId=?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, id);
            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("SQLErrors", ex.getMessage());
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("SQLErrors", e.getMessage());
            }
        }
        return false;
    }

    public static boolean insertFeedback(Feedback fb) {
        PreparedStatement stm = null;
        Connection con = null;

        try {
            con = DBUtils.makeConnection();
            String sql = "INSERT INTO FEEDBACK(userId, title, feedbackContent, sendDate) VALUES(?,?,?,?)";
            stm = con.prepareStatement(sql);
            stm.setInt(1, fb.getUserID());
            stm.setString(2, fb.getTitle());
            stm.setString(3, fb.getFeedbackContent());
            stm.setTimestamp(4, new java.sql.Timestamp(fb.getSendDate().getTime()));
            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("SQLErrors", ex.getMessage());
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("SQLErrors", e.getMessage());
            }
        }
        return false;
    }
}
